import com.greensock.TweenLite;

import display.FicheJoueur;

import flash.display.DisplayObject;
import flash.events.Event;
import flash.events.MouseEvent;

import model.Membre;

import mx.controls.Alert;
import mx.events.FlexEvent;
import mx.managers.PopUpManager;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;

public var fiche:FicheJoueur=new FicheJoueur();
[Bindable]
public var currentMember:Membre;
public var currentView:DisplayObject;

// EVT INTERFACE
// bouton
protected function btn_connection_clickHandler(event:MouseEvent):void
{
	btService.signIn(tiPseudo.text, tipass.text);
	
}
protected function btn_register_clickHandler(event:MouseEvent):void
{
	fiche.init(currentMember);
	PopUpManager.addPopUp(fiche,this,true);
	PopUpManager.centerPopUp(fiche);
}
protected function btn_deconnect_clickHandler(e:Event):void{
	btService.signOut();
	tipass.text="";
}
protected function siteTitle_clickHandler(event:MouseEvent):void
{
	if(currentView == accueil)
		return;
	currentView.visible=false;
	currentView = accueil;
	currentView.visible = true;
	//TweenLite.to(this.accueil,1,{rotationX:0, alpha:1,visible:true});
	
}

protected function menu_equipeAttente_clickHandler(event:MouseEvent):void
{
	if(currentView == waitingTeam)
		return;
	currentView.visible=false;
	currentView = waitingTeam;
	currentView.visible = true;
	//TweenLite.to(this.accueil,1,{rotationX:180, alpha:0,visible:false});
	
}

protected function menu_creerEquipe_clickHandler(event:MouseEvent):void
{
	// TODO Auto-generated method stub
	
}

protected function menu_dispo_clickHandler(event:MouseEvent):void
{
	// TODO Auto-generated method stub
	
}

protected function menu_joueursDispo_clickHandler(event:MouseEvent):void
{
	// TODO Auto-generated method stub
	
}

protected function evt_appComplete(event:FlexEvent):void
{
	fiche.addEventListener("SAVE",evt_fiche_save);
	currentView = accueil;
}

protected function evt_fiche_save(e:Event):void{
	btService.registerMember(fiche.currentMember);
}

// EVT SERVICE 

protected function signIn_resultHandler(e:ResultEvent):void{
	this.currentMember = e.result as Membre;
	if(this.currentMember !=null){
		this.setCurrentState("loggedIn");
	}
	
}
protected function signOut_resultHandler(e:ResultEvent):void{
	this.setCurrentState("loggedOut");
	this.currentMember = null;
	
}
protected function registerUser_resultHandler(e:ResultEvent):void{
	if(e.result == "OK")
	{
		currentMember = fiche.currentMember;
		this.setCurrentState("loggedIn");
	}
	
}
protected function btService_faultHandler(e:FaultEvent):void{
	Alert.show("erreur:"+e.message.toString());
}

